package com.stedu.mall.adminservice.servicer;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.adminservice.mapper.AdminMapper;
import com.stedu.mall.common.bean.Admin;
import com.stedu.mall.common.exception.MallException;
import com.stedu.mall.common.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin login(String username, String password) throws MallException {
        //根据用户名查询
        Admin admin = adminMapper.selectByUsername(username);
        if (admin == null){
            throw new MallException("用户名不存在");
        }
        //判断是否禁用
        if (admin.getStatus().equals(1)) {
            throw new MallException("您被其他管理员禁用，请联系其他管理员");
        }
        //对用户输入的密码进行加密 - 使用MD5算法和盐值生成
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(password + admin.getSalt()));
        if (!md5Pwd.equals(admin.getPassword())){
            throw new MallException("密码错误");
        }
        //对加密之后的密码和数据库中的密码进行比较
        return admin;
    }

    @Override
    public Boolean chgPwd(String oldPwd, String newPwd, Integer id) throws MallException {
        //判断原密码是否正确
        Admin admin = adminMapper.selectById(id);
        String salt = admin.getSalt();
        String md5InputOldPwd = SecureUtil.md5(SecureUtil.md5(oldPwd + salt));
        //和数据库中的密码进行比较
        if (!admin.getPassword().equals(md5InputOldPwd)) {
            throw new MallException("原密码输入错误，请输入后重新输入");
        }
        //判断新密码和原密码是否一样
        //修改密码 结合盐进行加密
        String md5NewPwd = SecureUtil.md5(SecureUtil.md5(newPwd + salt));
        admin = new Admin();
        admin.setId(id);
        admin.setPassword(md5NewPwd);
        return adminMapper.update(admin) ==1;
    }

    @Override
    public Boolean insert(Admin admin) throws MallException {
      return adminMapper.insert(admin) == 1;
    }
    @Override
    public Boolean delete(Integer id) throws MallException {
        return adminMapper.delete(id) == 1;

    }

    @Override
    public Boolean update(Admin admin) throws MallException {
        List<Admin> adminList = adminMapper.selectByCondition(admin);
        //用户名不能相同
        if (adminList.stream()
                .anyMatch(item -> item.getUsername().equals(admin.getUsername()) && !item.getId().equals(admin.getId()))) {
            throw new MallException("用户名相同不能修改");
        }
        //判断是否需要修改密码
        if (admin.getPassword()!= null && !admin.getPassword().isEmpty()) {
            if (!admin.getPassword().equals("123")) {
                throw new MallException("无法重置密码");
            }
            //查询盐进行加密
            Admin oldAdmin = adminMapper.selectById(admin.getId());
            String salt = oldAdmin.getSalt();
            String md5InputNewPwd = SecureUtil.md5(SecureUtil.md5(admin.getPassword() + salt));
            admin.setPassword(md5InputNewPwd);
        }

        return adminMapper.update(admin) == 1;
    }

    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectById(id);
    }


    @Override
    public PageInfo<Admin> selectByCondition(Admin condition, Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //查询
        List<Admin> admin = adminMapper.selectByCondition(condition);
        PageInfo<Admin> pageInfo = new PageInfo<>(admin);
        return pageInfo;
    }
}
